﻿using BLL.Basis;
using CommandService;
using Entity.ViewEntity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using WebProject.Common;

namespace WebProject
{
    public class MvcApplication : System.Web.HttpApplication
    {
        Tools.TxtLogHelper txt = new Tools.TxtLogHelper();
        string autoTime = Tools.ConfigReader.Reader["AutoTaskTime"];
        string isStart = Tools.ConfigReader.Reader["AutoTask"];
        Commands cmd;
        private void AutoTask()
        {
            try
            {
                BLL.AutoTaskBLL autoBLL = new BLL.AutoTaskBLL();
                var rsl = autoBLL.AutoTask();
                if (rsl.Result)
                {
                    var list = (List<Dictionary<string, string>>)rsl.Data;
                    if (list.Count > 0)
                    {
                        string par = list[0]["parValue"];
                        if (!string.IsNullOrWhiteSpace(par))
                        {
                            var obj = JsonConvert.DeserializeObject<JObject>(par);
                            isStart = obj["start"].ToString();
                            autoTime = obj["runtime"].ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                txt.AddLogMsg("方法执行错误(AutoTask):" + ex.Message);
            }

        }

        protected void Application_Start()
        {
            cmd = new Commands();

            AutoTask();

            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            WebTimeTask.Instance().ExecuteTask += new System.Timers.ElapsedEventHandler(Global_ExecuteTask);
            WebTimeTask.Instance().Interval = 1000 * 60 * int.Parse(autoTime);//间隔时间
            WebTimeTask.Instance().Start();

        }
        void Global_ExecuteTask(object sender, System.Timers.ElapsedEventArgs e)
        {
            //下面的代码是关键，可解决IIS应用程序池自动回收的问题
            BLL.AutoTaskBLL autoBLL = new BLL.AutoTaskBLL();
            try
            {

                if (isStart == "1")
                {
                    autoBLL.auto_token();
                    autoBLL.auto_saveBar();//自动入库 入库失败的条码
                    autoBLL.Auto_Dept();
                    autoBLL.Auto_User();
                    autoBLL.auto_WareHouse();
                    //autoBLL.auto_WarePosition();//货位
                    //autoBLL.auto_UnitInfo();//计量单位
                    autoBLL.auto_Product();
                    //autoBLL.auto_Supplier();//供应商
                    autoBLL.auto_customer();
                    autoBLL.ClearSSORecord();
                    //autoBLL.SyncProductCommonData();
                    //autoBLL.SyncProductDefCommonData();
                }
            }
            catch (Exception ex)
            {
                txt.AddLogMsg("自动同步任务:" + ex.Message);
            }

        }
        void Application_End(object sender, EventArgs e)
        {

            //  在应用程序关闭时运行的代码

            //如果出错，删除下面代码

            //下面的代码是关键，可解决IIS应用程序池自动回收的问题

            System.Threading.Thread.Sleep(1000);
            string url = Tools.ConfigReader.Reader["SystemUrl"];
            System.Net.HttpWebRequest myHttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            System.Net.HttpWebResponse myHttpWebResponse = (System.Net.HttpWebResponse)myHttpWebRequest.GetResponse();
            System.IO.Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流
        }

    }
}

